<?php

//	Pass remote desktop: S@ngeL1

// $hostname="1.234.91.160";					// Server Korea
$hostname="112.213.84.141";					// Server Vietnam
$username="cam";
$password="5678";
$databasename="shopping";

// $api_key = "AIzaSyAVLc8QahfI3F3M3B87IfIMBCB9YdssoFI";
$api_key = "AIzaSyC1tpZ_RPGgo9qtmCalRvLGO72hH9ozt-4";

date_default_timezone_set("Asia/Saigon");

class Notify {
	public $id;
	public $id_mem;
	public $id_sender;
	public $uid_sender;
	public $name_sender;
	public $id_owner;
	public $uid_owner;
	public $name_owner;
	public $content;
	public $id_post;
	public $image;
	public $type;
	public $cm_type;
	public $unread;
	public $date;
	public $status;
	
	public function __construct($id, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cm_type, $unread, $date, $status) {
		$this->id = $id;
		$this->id_mem = $id_mem;
		$this->id_sender = $id_sender;
		$this->uid_sender = $uid_sender;
		$this->name_sender = $name_sender;
		$this->id_owner = $id_owner;
		$this->uid_owner = $uid_owner;
		$this->name_owner = $name_owner;
		$this->content = $content;
		$this->id_post = $id_post;
		$this->image = $image;
		$this->type = $type;
		$this->cm_type = $cm_type;
		$this->unread = $unread;
		$this->date = $date;
		$this->status = $status;
	}
}


function TestPDO() {
// $conn = mysql_connect($hostname, $username, $password) or
// die("Khong ket noi dc Database !");
// mysql_select_db($databasename);
// mysql_query("SET NAMES 'utf8'");
    $dbo = new PDO("mysql:host=$hostname;dbname=$databasename", "$username", "$password");
    $dbo->exec("SET NAMES 'utf8'");
// var_dump($dbo);
    $result9 = "";
    try {
        $str1 = "SELECT	`name`
                FROM	`rate`
                WHERE	`id`=4;";
        $result7 = $dbo->query($str1);
        $result8 = $result7->fetchAll(PDO::FETCH_ASSOC);
        $result9 = $result8[0]['name'];
    } catch (PDOException $ex) {
        $dbo = null;
        echo $ex->getMessage();
        return;
    }
    $dbo = null;

    return $result9;
}


function NotifyComment($idcomment, $cmtype) {
	global $hostname;
	global $username;
	global $password;
	global $databasename;
	
	$conn = mysql_connect($hostname, $username, $password) or
		die("Khong ket noi dc Database !");
	mysql_select_db($databasename);
	mysql_query("SET NAMES 'utf8'");
	
	$str = "";
	
	if ($cmtype==1) {
		$str = "SELECT		c.`content`,
							a.`id`			AS	`id_mem`,
							a.`gcm_id`,
							a1.`id`			AS	`id_sender`,
							a1.`name`		AS	`uid_sender`,
							a1.`nick_name`	AS	`name_sender`,
							a1.`image`,
							p.`id_category`	AS	`id_owner`,
							p.`name`		AS	`uid_owner`,
							p.`name`		AS	`name_owner`,
							c1.`id_product`	AS	`id_post`,
							c.`date`
				FROM		`comment` c
				INNER JOIN	`product` p
				ON			p.`id`=c.`id_product`
				INNER JOIN	`comment` c1
				ON			c1.`id_product`=c.`id_product`
				INNER JOIN	`account` a
				ON			a.`id`=c1.`id_account`
				INNER JOIN	`account` a1
				ON			a1.`id`=c.`id_account`
				WHERE		c.`id`=$idcomment
				GROUP BY	a.`id`
				HAVING		a.`id`<>a1.`id`;";
		
		$rs4 = mysql_query($str);
		if (mysql_num_rows($rs4)>0) {
			$rs5 = mysql_fetch_assoc($rs4);
			$id_mem = $rs5['id_mem'];
			$id_sender = $rs5['id_sender'];
			$uid_sender = $rs5['uid_sender'];
			$name_sender = $rs5['name_sender'];
			$id_owner = $rs5['id_owner'];
			$uid_owner = $rs5['uid_owner'];
			$name_owner = $rs5['name_owner'];
			$content = $rs5['content'];
			$id_post = $rs5['id_post'];
			$image = $rs5['image'];
			$type = 5;
			$unread = 1;
			$date = $rs5['date'];
			$status = 1;
			
			$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
					VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '$cmtype', '$unread', '$date', '$status');";
					
			$rs6 = mysql_query($str);
			if ($rs6==TRUE) {
				$ids = array();
				$ids[] = $rs5['gcm_id'];
				$id1 = mysql_insert_id();
				$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
				$mess = json_encode($notify);
				SendNotification($mess, $ids);
			}
		}
	}
	
	if ($cmtype==2) {
		$str = "SELECT		c1.`content`,
							a.`id`			AS	`id_mem`,
							a.`gcm_id`,
							a1.`id`			AS	`id_sender`,
							a1.`name`		AS	`uid_sender`,
							a1.`nick_name`	AS	`name_sender`,
							a1.`image`,
							a2.`id`			AS	`id_owner`,
							a2.`name`		AS	`uid_owner`,
							a2.`nick_name`	AS	`name_owner`,
							c1.`id_product`	AS	`id_post`,
							c.`date`
				FROM		`comment` c
				INNER JOIN	`comment` c1
				ON			c1.`id_product`=c.`id_product`
				INNER JOIN	`account` a1
				ON			a1.`id`=c1.`id_account`
				INNER JOIN	`post` p
				ON			p.`id`=c1.`id_product`
				INNER JOIN	`account` a2
				ON			a2.`id`=p.`id_account`
				INNER JOIN	`account` a
				ON			(a.`id`=p.`id_account`)
				WHERE		c1.`id`=$idcomment
				GROUP BY	a.`id`
				HAVING		a.`id`<>a1.`id`;";
				
		$rs4 = mysql_query($str);
		if (mysql_num_rows($rs4)>0) {
			$rs5 = mysql_fetch_assoc($rs4);
			$id_mem = $rs5['id_mem'];
			$id_sender = $rs5['id_sender'];
			$uid_sender = $rs5['uid_sender'];
			$name_sender = $rs5['name_sender'];
			$id_owner = $rs5['id_owner'];
			$uid_owner = $rs5['uid_owner'];
			$name_owner = $rs5['name_owner'];
			$content = $rs5['content'];
			$id_post = $rs5['id_post'];
			$image = $rs5['image'];
			$type = 1;
			$unread = 1;
			$date = $rs5['date'];
			$status = 1;
			
			$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
					VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '$cmtype', '$unread', '$date', '$status');";
					
			$rs6 = mysql_query($str);
			if ($rs6==TRUE) {
				$ids = array();
				$ids[] = $rs5['gcm_id'];
				$id1 = mysql_insert_id();
				$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
				$mess = json_encode($notify);
				SendNotification($mess, $ids);
			}
		}
		
		$str = "SELECT		c.`content`,
							a.`id`			AS	`id_mem`,
							a.`gcm_id`,
							a1.`id`			AS	`id_sender`,
							a1.`name`		AS	`uid_sender`,
							a1.`nick_name`	AS	`name_sender`,
							a1.`image`,
							a2.`id`			AS	`id_owner`,
							a2.`name`		AS	`uid_owner`,
							a2.`nick_name`	AS	`name_owner`,
							c1.`id_product`	AS	`id_post`,
							c1.`date`
				FROM		`comment` c
				INNER JOIN	`post` p
				ON			p.`id`=c.`id_product`
				INNER JOIN	`comment` c1
				ON			c1.`id_product`=c.`id_product`	AND
							c1.`id_account`<>p.`id_account`
				INNER JOIN	`account` a
				ON			a.`id`=c1.`id_account`
				INNER JOIN	`account` a1
				ON			a1.`id`=c.`id_account`
				INNER JOIN	`account` a2
				ON			a2.`id`=p.`id_account`
				WHERE		c.`id`=$idcomment
				GROUP BY	a.`id`
				HAVING		a.`id`<>a1.`id`;";
		
		$rs4 = mysql_query($str);
		if (mysql_num_rows($rs4)>0) {
			$rs5 = mysql_fetch_assoc($rs4);
			$id_mem = $rs5['id_mem'];
			$id_sender = $rs5['id_sender'];
			$uid_sender = $rs5['uid_sender'];
			$name_sender = $rs5['name_sender'];
			$id_owner = $rs5['id_owner'];
			$uid_owner = $rs5['uid_owner'];
			$name_owner = $rs5['name_owner'];
			$content = $rs5['content'];
			$id_post = $rs5['id_post'];
			$image = $rs5['image'];
			$type = 2;
			$unread = 1;
			$date = $rs5['date'];
			$status = 1;
			
			$str = "INSERT INTO	`notify`	(`id_mem`, `id_sender`, `uid_sender`, `name_sender`, `id_owner`, `uid_owner`, `name_owner`, `content`, `id_post`, `image`, `type`, `cm_type`, `unread`, `date`, `status`)
					VALUES					('$id_mem', '$id_sender', '$uid_sender', '$name_sender', '$id_owner', '$uid_owner', '$name_owner', '$content', '$id_post', '$image', '$type', '$cmtype', '$unread', '$date', '$status');";
					
			$rs6 = mysql_query($str);
			if ($rs6==TRUE) {
				$ids = array();
				$ids[] = $rs5['gcm_id'];
				$id1 = mysql_insert_id();
				$notify = new Notify($id1, $id_mem, $id_sender, $uid_sender, $name_sender, $id_owner, $uid_owner, $name_owner, $content, $id_post, $image, $type, $cmtype, $unread, $date, $status);
				$mess = json_encode($notify);
				echo SendNotification($mess, $ids);
			}
		}
	}
	
	mysql_close($conn);
	return 1;
}



function SendNotification($message, $registrationIDs) {
	global $api_key;
	
	// Set POST variables
	$url = 'https://android.googleapis.com/gcm/send';
	
	$fields = array(
					'registration_ids'  => $registrationIDs,
					'data'              => array( "message" => $message ),
					);

	$headers = array( 
					'Authorization: key=' . $api_key,
					'Content-Type: application/json'
					);

	// Open connection
	$ch = curl_init();

	// Set the url, number of POST vars, POST data
	curl_setopt( $ch, CURLOPT_URL, $url );

	curl_setopt( $ch, CURLOPT_POST, true );
	curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

	curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $fields ) );

	curl_setopt ($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cacert.pem");
	curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
	// Execute post
	$result = curl_exec($ch);

	// Close connection
	curl_close($ch);

	return $result;
}



if(isset($_REQUEST['action'])){
	if($_REQUEST['action']=="NotifyComment" ){
		NotifyComment(871, 2);
	}
}